﻿using System;
using System.Windows;
using System.Windows.Input;
using MaxAlarm.Common;
using MaxAlarm.Common.WebQueries;
using MaxAlarm.Core.Authorization;
using MaxAlarm.Core.Settings;
using MaxAlarm.Pages;

namespace MaxAlarm.ViewModels
{
    public class LoginPageViewModel : DependencyObject
    {
        #region Properties

        public static readonly DependencyProperty LoginProperty =
            DependencyProperty.Register("Login", typeof (string), typeof (LoginPageViewModel), new PropertyMetadata(null));

        public string Login
        {
            get { return (string) GetValue(LoginProperty); }
            set { SetValue(LoginProperty, value); }
        }

        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(LoginPageViewModel), new PropertyMetadata(null));

        public string Password
        {
            get { return (string) GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }

        
        public static readonly DependencyProperty HasErrorProperty =
            DependencyProperty.Register("HasError", typeof (bool), typeof (LoginPageViewModel), new PropertyMetadata(default(bool)));

        public bool HasError
        {
            get { return this.SafeGetValue<bool>(HasErrorProperty); }
            set { this.SafeSetValue(HasErrorProperty, value); }
        }

        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register("ErrorMessage", typeof (string), typeof (LoginPageViewModel), new PropertyMetadata(default(string)));

        public string ErrorMessage
        {
            get { return this.SafeGetValue<string>(ErrorMessageProperty); }
            set { this.SafeSetValue(ErrorMessageProperty, value); }
        }

        public static readonly DependencyProperty SavePasswordProperty =
            DependencyProperty.Register("SavePassword", typeof (bool), typeof (LoginPageViewModel), new PropertyMetadata(true));

        public bool SavePassword
        {
            get { return (bool) GetValue(SavePasswordProperty); }
            set { SetValue(SavePasswordProperty, value); }
        }

        public static readonly DependencyProperty PageManagerProperty =
            DependencyProperty.Register("PageManager", typeof (PageManager), typeof (LoginPageViewModel), new PropertyMetadata(default(PageManager)));

        public PageManager PageManager
        {
            get { return (PageManager) GetValue(PageManagerProperty); }
            set { SetValue(PageManagerProperty, value); }
        }

        #endregion

        #region Commands

        public static ICommand LoginCommand { get; private set; }

        private static void LoginExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var page = (LoginPage) sender;
            var vm = page.ViewModel;

            vm.HasError = false;
            vm.ErrorMessage = string.Empty;

            if(string.IsNullOrEmpty(vm.Login) || string.IsNullOrEmpty(vm.Password))
            {
                vm.HasError = true;
                vm.ErrorMessage = "Login and password fields must not be empty!";
                return;
            }

            LoginManager.Instance.Login = vm.Login;
            LoginManager.Instance.Password = vm.Password;
            LoginManager.Instance.LoginAsync();
        }

        public static ICommand CancelCommand { get; private set; }

        private static void CancelExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            Application.Current.MainWindow.Close();
        }

        #endregion

        static LoginPageViewModel()
        {
            LoginCommand = new RoutedCommand("Login", typeof (LoginPageViewModel));
            CancelCommand = new RoutedCommand("Cancel", typeof (LoginPageViewModel));

            CommandManager.RegisterClassCommandBinding(typeof(LoginPage), new CommandBinding(LoginCommand, LoginExecuted));
            CommandManager.RegisterClassCommandBinding(typeof(LoginPage), new CommandBinding(CancelCommand, CancelExecuted));
            CommandManager.RegisterClassInputBinding(typeof(LoginPage), new KeyBinding(LoginCommand, Key.Enter, ModifierKeys.None));
            CommandManager.RegisterClassInputBinding(typeof(LoginPage), new KeyBinding(CancelCommand, Key.Escape, ModifierKeys.None));
        }

        public LoginPageViewModel()
        {
            LoginManager.Instance.Error += Error;
            LoginManager.Instance.Success += Success;
        }

        void Success(object sender, WebQueryResult result)
        {
            if (SavePassword)
                SettingsManager.Instance.Settings.AccountsSettings[Login] = Password;
            else
                SettingsManager.Instance.Settings.AccountsSettings[Login] = string.Empty;

            SettingsManager.Instance.SaveSetting();

            PageManager.CurrentPage = PageManager.MainPage;
        }

        void Error(object sender, Exception exception)
        {
            HasError = true;
            ErrorMessage = exception.Message;
        }

    }
}
